package com.lsc.learn.mybatisplus.componet;

import java.util.ArrayList;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

import lombok.Getter;
import lombok.Setter;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnProperty(prefix = "swagger2",value = {"enable"}, havingValue = "true")
@ConfigurationProperties(value = "swagger2", ignoreInvalidFields = true)
@Getter
@Setter
public class SwaggerAqiConfig {
	
	String enable;
	String basePackage;
	String groupName = "默认分组";
	String title = "数据接口";
	String description = "数据接口";
	String version = "1.0";
	String termsOfServiceUrl = "termsOfServiceUrl";
	String contactName = "contactName";
	String contactUrl = "contactUrl";
	String contactEmail = "contactEmail";
	String license = "license";
	String licenseUrl = "licenseUrl";
	
    @Bean
    @ConditionalOnProperty(prefix = "swagger2",value = {"basePackage"})
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/*.*"))//过滤的接口
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .build()
                .groupName(groupName)
                .apiInfo(apiInfo());
    }
    
    private ApiInfo apiInfo() {
        Contact contact = new Contact(contactName, contactUrl, contactEmail);
        ApiInfo apiInfo = new ApiInfo(title,//大标题
        		description,//小标题
                version,//版本
                termsOfServiceUrl,
                contact,//作者
                license,//链接显示文字
                licenseUrl,//网站链接
                new ArrayList<>()
        );
        
        return apiInfo;
    }
    
}
